package com.mmd.manage.service.impl;

import com.mmd.bean.CODE;
import com.mmd.bean.PageBean;
import com.mmd.bean.UserLoginContext;
import com.mmd.common.entity.MmdProduct;
import com.mmd.exception.GameException;
import com.mmd.intercepter.AdminInterceptor;
import com.mmd.manage.dao.MmdProductDao;
import com.mmd.manage.service.ProductService;
import com.mmd.manage.vo.MmdProductPageEo;
import com.mmd.utils.UniqueNoUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private MmdProductDao mmdProductDao;

    @Override
    public PageBean<List<MmdProduct>> getMmdProductPageList(MmdProductPageEo mmdProductPageEo) {
        mmdProductPageEo.setUid(AdminInterceptor.getUserLoginContext().getUid());
        List<MmdProduct> list = mmdProductDao.getMmdProductPageList(mmdProductPageEo);
        return new PageBean(list, mmdProductDao.getMmdProductPageListTotal(mmdProductPageEo),mmdProductPageEo.getPageSize(), mmdProductPageEo.getPageNo());
    }

    @Override
    public MmdProduct getMmdProductById(Long id) {
        return mmdProductDao.getById(id);
    }

    @Override
    public void addMmdProduct(MmdProduct mmdProduct) {
        if (StringUtils.isEmpty(mmdProduct.getProductEnName())){
            throw new GameException(new CODE(9999, "请输入产品英文名"));
        }
        if (StringUtils.isEmpty(mmdProduct.getHsCode())){
            throw new GameException(new CODE(9999, "请输入HSCode"));
        }
        if (mmdProduct.getDeclareUnitPrice() == null){
            throw new GameException(new CODE(9999, "请输入产品申报价"));
        }

        UserLoginContext userLoginContext = AdminInterceptor.getUserLoginContext();

        MmdProduct uniqueProduct = mmdProductDao.getUniqueMmdProduct(null, userLoginContext.getUid(), mmdProduct.getProductEnName(), mmdProduct.getHsCode(), mmdProduct.getDeclareUnitPrice());
        if (uniqueProduct != null){
            throw new GameException(new CODE(9999, "已存在相同的英文名、HSCode、申报价的产品"));
        }

        mmdProduct.setPid(UniqueNoUtils.next(UniqueNoUtils.UniqueNoType.P));
        mmdProduct.setUid(userLoginContext.getUid());
        mmdProduct.setCreateTime(new Date());
        mmdProductDao.insert(mmdProduct);
    }

    @Override
    public void modifyMmdProduct(MmdProduct mmdProduct) {
        if (mmdProduct.getId() == null){
            throw new GameException(new CODE(9999, "请选择产品"));
        }
        if (StringUtils.isEmpty(mmdProduct.getProductEnName())){
            throw new GameException(new CODE(9999, "请输入产品英文名"));
        }
        if (StringUtils.isEmpty(mmdProduct.getHsCode())){
            throw new GameException(new CODE(9999, "请输入HSCode"));
        }
        if (mmdProduct.getDeclareUnitPrice() == null){
            throw new GameException(new CODE(9999, "请输入产品申报价"));
        }

        UserLoginContext userLoginContext = AdminInterceptor.getUserLoginContext();
        MmdProduct oldMmdProduct = mmdProductDao.getById(mmdProduct.getId());
        if (userLoginContext.getUid() != oldMmdProduct.getUid()){
            throw new GameException(new CODE(9999, "您无法修改此商品"));
        }

        MmdProduct uniqueProduct = mmdProductDao.getUniqueMmdProduct(mmdProduct.getId(), userLoginContext.getUid(), mmdProduct.getProductEnName(), mmdProduct.getHsCode(), mmdProduct.getDeclareUnitPrice());
        if (uniqueProduct != null){
            throw new GameException(new CODE(9999, "已存在相同的英文名、HSCode、申报价的产品"));
        }

        mmdProduct.setUid(userLoginContext.getUid());
        mmdProduct.setUpdateTime(new Date());
        mmdProductDao.updateById(mmdProduct);
    }

    @Override
    public void deleteMmdProduct(Long id) {
        if (id == null){
            throw new GameException(new CODE(9999, "请选择产品"));
        }
        UserLoginContext userLoginContext = AdminInterceptor.getUserLoginContext();
        MmdProduct oldMmdProduct = mmdProductDao.getById(id);
        if (userLoginContext.getUid() != oldMmdProduct.getUid()){
            throw new GameException(new CODE(9999, "您无法修改此商品"));
        }
        mmdProductDao.deleteById(id);
    }
}
